package controllers;

import bean.Attribute;
import bean.AttributeKeyValue;
import bean.MarketingAttributeDto;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import play.Logger;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;

import java.io.IOException;
import java.util.*;

/**
 * This controller contains an action to handle HTTP requests
 * to the application's home page.
 */
public class HomeController extends Controller {

    /**
     * An action that renders an HTML page with a welcome message.
     * The configuration in the <code>routes</code> file means that
     * this method will be called when the application receives a
     * <code>GET</code> request with a path of <code>/</code>.
     */
    public Result index() {
		System.out.println("test........");
        return ok(views.html.test.render());
//        return ok(index.render("龙化神"));
//        return ok(index.render("Your new application is ready."));
    }

    public Result toTest() {
        return ok(views.html.test2.render());
    }

    public Result test() {
        Map<String, String[]> map = request().body().asFormUrlEncoded();
        play.Logger.info("map:" + map);
        return ok(views.html.test2.render());
    }

    public Result myTest() {
        return ok(views.html.test.render());
    }

    public Result getOptions() {
        List<MarketingAttributeDto> marketingAttributeDtoList = new ArrayList<>();
        MarketingAttributeDto marketingAttributeDto = new MarketingAttributeDto();

        //模拟颜色
        List<Attribute> attributes = new ArrayList<>();

        Attribute attribute = new Attribute();
        attribute.setName("红色");
        attribute.setFlag(1);
        attributes.add(attribute);

        Attribute attribute1 = new Attribute();
        attribute1.setName("黄色");
        attribute1.setFlag(0);
        attributes.add(attribute1);

        Attribute attribute2 = new Attribute();
        attribute2.setName("蓝色");
        attribute2.setFlag(0);
        attributes.add(attribute2);

        marketingAttributeDto.setName("颜色");
        marketingAttributeDto.setAttType(4);
        marketingAttributeDto.setOptions(attributes);

//        <tr>
//        <th>适宜人群</th>
//        <td>
//        <select class="auto" name="attr_id_1">
//        <option value="少年">少年</option>
//        <option value="老年">老年</option>
//        <option value="儿童">儿童</option>
//        </select>
//
//        </td>
//        </tr>
        for (Attribute attributeBean : attributes) {

        }
        String selectContect = "<select class=\"auto\" name=\"attr_5\">\n" +
                "        <option value=\"少年\">少年</option>\n" +
                "        <option value=\"老年\">老年</option>\n" +
                "        <option value=\"儿童\">儿童</option></select>";
        selectContect += "日期：<input type=\"text\" name=\"attr_1\" value=\"2a354\">";
        StringBuffer sb = new StringBuffer();
        sb.append("<tr>")
                .append("<th>").append("颜色").append("</th>").
                append("<td>").append(selectContect).append("</td>").append("<tr>");

        return ok(sb.toString());
    }

    //根据listeingid生成已绑定的表单


    public Result saveAttrSet() {
//        Map<String, String[]> map = request().body().asFormUrlEncoded();
//
//        String[] assetArray = map.get("asset");
//        for (int i = 0;i < assetArray.length;i++) {
//            System.out.println(assetArray[i]);
//        }
//        Logger.info("map:" + map);
        JsonNode jsonNode = request().body().asJson();
        Logger.info("jsonNode:" + jsonNode);

        String listingId = jsonNode.findPath("listingId").textValue();
        ObjectMapper mapper = new ObjectMapper();
        try {
            String jsonArr = jsonNode.findPath("asset").textValue();
            List<LinkedHashMap<String, Object>> list = mapper.readValue(
                    jsonArr, List.class);
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Set<String> set = map.keySet();
                for (Iterator<String> it = set.iterator(); it.hasNext();) {
                    String key = it.next();
                    System.out.println(key + ":" + map.get(key));
                }


            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return ok();
    }
}
